//
//  KotnetAvailable.m
//  KotnetAutologin
//
//	check kotnet is available
//
//  Created by Kristof Marien on 14/01/10.
//  Copyright 2010 KotnetAutologin. All rights reserved.
//

#import "KotnetAvailable.h"


@implementation KotnetAvailable

@synthesize delegate;
@synthesize availableCallback;
@synthesize unavailableCallback;

/**
 *  starts checking
 */
- (void)checkIsAvailable:(id)checkDelegate availableSelector:(SEL)availableSelector  unavailableSelector:(SEL)unavailableSelector
{
	self.delegate = checkDelegate;
	self.availableCallback = availableSelector;
	self.unavailableCallback = unavailableSelector;
	
	source = [GetHTMLSource alloc];
	source.url = @"https://netlogin.kuleuven.be";
	[source getSource:self receivedSelector:@selector(sourceReceived:) errorSelector:@selector(sourceNotReceived)];
}
/**
 *  source is received, check it
 */
- (void)sourceReceived:(NSString *)data
{
	[source release];
	NSRange range = [data rangeOfString:@"kotnet"];
	if (range.location == NSNotFound) {
		[self sourceNotReceived];
	} else {
		if (self.delegate && self.availableCallback) {
			if ([self.delegate respondsToSelector:self.availableCallback]) {
				[self.delegate performSelector:self.availableCallback];
			}
		}
	}
}
/**
 *  error loading source
 */
- (void)sourceNotReceived
{
	if (self.delegate && self.unavailableCallback) {
		if ([self.delegate respondsToSelector:self.unavailableCallback]) {
			[self.delegate performSelector:self.unavailableCallback];
		}
	}
}
@end
